Billede

Oliver Sturm had an interesting talk on day 3 of SDD Conference. He talked about the event sourcing architecture, that can be used for highly performing and error resilient communication in a software system (applicable to both SOA and Microservice applications).

Event sourcing

Event sourcing is an architectural pattern that records all changes to an application's state as a sequence of events. These events are then stored in an event store - usually a database that is optimized for storing and querying events.

Event store

When an event is created, it is assigned a unique identifier and a timestamp. The event also includes information about the entity that was changed, the type of change that was made, and the old and new values of the entity.

The event store is typically append-only, which means that events can only be added to the end of the log. This makes it very efficient to query the event store, as only the most recent events need to be scanned.

A normal pattern is to have an endpoint that receives all requests coming in to the system and saving them in the event store. This is a database that holds all the requests that comes in to the system, so that they can be inspected at a later time if neccesary. The event store will send a message to the event bus, when the request has been saved. Normally the data fra the request is not changed in anyway ie. the data saved to the event store database is exactly the same, that is sent to the event bus.

Read model

A read model is a representation of the current state of an application that is derived from the event stream coming from the event bus (ie. the messages that was forwarded from the event store service). The read model is used to provide a consistent view of the application state to clients, even if the application has been updated multiple times since the client last requested data.

A read model will usually be responsible for a subset of the data. This could be customers, the stock in the warehouse or something completely else. This means that there will be multiple read model, each responsible for handling queries regarding a subset of the data. The read model is populated by replaying the event stream - but only the events that relate to the entities that is relevant for that read model (eg. changes to customers). This means that the event store is read from start to finish, and each event is used to update the read model.

Using VAR for failure recovery 

The event stream provides the possiblity to replay everything that happend from a certain point in time (or from the beginning if needed). This means that there's always the option to use the event messages as VAR in football, where we can replay everything that has happend over and over again (and debug it if we need to). 

An error happend due to a bug in the code, can be fixed and when the fix has been deployed, the event stream can be replayed and the data can be verified. If everything looks to be in order, we can drop the old read model database, create a new database and replay the event stream to repopulate the new database with the correct data.

Event sourcing framework for C#

The best event sourcing framework for you will depend on your specific needs and requirements. Here's a few of the most popular frameworks for C#

Axon Framework

Axon Framework is a complete event sourcing framework that provides a number of features, including event sourcing, command bus, query bus, and event store. If you are looking for a complete framework with a lot of features, then Axon Framework is a good option. 

EventStore.NET

EventStore.NET is an event store that can be used with any event sourcing framework. It provides a number of features, including append-only storage, event compaction, and event replication. If you are looking for an event store that can be used with any event sourcing framework, then EventStore.NET is a good option.

SimpleCQRS

SimpleCQRS is a lightweight event sourcing framework that is easy to use. It provides a number of features, including command bus, query bus, and event store. If you are looking for a lightweight framework that is easy to use, then SimpleCQRS is a good option.